var common = require('../../utils/common.js');
var ajax = require('../../utils/ajax.js');
var Base = require('../../common/CmptBase.js');

var MSearch = function({
  page,
  scope,
  cmpt
}) {
  Base.apply(this, arguments);
  this.init();
}

common.extend(MSearch, Base);

Object.assign(MSearch.prototype, {
  init(optinos) {
    optinos = optinos || {};
    
    this._onSearch = optinos.onSearch;

    this.setData({
      q: '',
      isClear: false,
      placeholder: optinos.placeholder || '',
    });

    return this;
  },

  input(evt) {
    var val = evt.detail.value;
    var isClear = !!val;

    this.setData({
      q: evt.detail.value,
      isClear,
    })
  },

  blur(evt) {
    var val = evt.detail.value;

    if (!val) {
      this.setData({
        isClear: false
      });
    }
  },

  focus(evt) {
    this.setData({
      isClear: true
    });
  },

  clear() {
    this.setData({
      q: '',
      isClear: false
    });
  },

  search(evt) {
    this._onSearch && this._onSearch(this.getData('q'));
  },

});

module.exports = MSearch;